'''
Created on 01.04.2011

@author: bstar
'''
import argparse 
from dopplershift.core.app import ApplicationBuilder
import os


class CommandLineApp(ApplicationBuilder):
    __program__ = 'BaseCommand'
    __description__ = 'Base Description'
    __arguments__ = (
                     
                     )
    without_configfile = False
    def __init__(self, *args, **kwargs):
        self._commandLineParser()
    def _application_config(self, value):
        msg = ''
        if not os.path.isfile(value):
            msg = 'Need specified path to file for configfile option.'
        if not os.access(value, os.R_OK):
            msg += 'Configfile object must be readable.'
        if msg != '':
            raise argparse.ArgumentTypeError(msg)
        return value
            
    def _commandLineParser(self):
        parser = argparse.ArgumentParser(prog=self.__program__, description=self.__description__ )
        if self.without_configfile != True:
            parser.add_argument('configfile', metavar='ConfigFile', type=self._application_config, help='full path on application config file')
        for el in self.__arguments__:
            name = el.pop('name')
            parser.add_argument(
                                name, **el
                                )
            
        self._args = parser.parse_args()
        
